<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>Hello APP</title>
		<link href="./css/bootstrap.min.css"  rel="stylesheet"/>
		<link href="./css/bootstrap-theme.min.css" rel="stylesheet"/>
	</head>
	<body>
		<div class="jumbotron">
			<div class="container">
				<h1>APPCloud <small>异步文件上传示例</small></h1>
				<p>
					也就这样了……
				</p>
			</div>
		</div>
		<div class="container">
			<form  id="uploadfile" action="http://192.168.99.157/app/api/data/SaveFile" enctype="multipart/form-data" method="post">
				<div class="form-group">
					<label for="exampleInputUserid">用户ID</label>
					<input type="text" class="form-control" id="exampleInputUserid" name="exampleInputUserid" placeholder="userid" value="master" />
				</div>
				<div class="form-group">
					<label for="exampleInputFile">文件选择</label>
					<input type="file" id="exampleInputFile" name="exampleInputFile">
					<p class="help-block">
						这里写一些帮助文字，也是可以的
					</p>
				</div>
				<a href="javascript:;" type="button" class="btn btn-primary" onclick="uploadfile()" id="btnupload"> 提交 </a>
			</form>
		</div>
		<script type="text/javascript" src="./script/api.js"></script>
		<script type="text/javascript">
			apiready = function() {
				console.log("Hello APICloud");
				var chromeDebug = api.require('chromeDebug');
				chromeDebug.openDebug(function(ret, err) {
					if (212 == ret.code) {
						alert(JSON.stringify(ret));
					} else {
						alert(JSON.stringify(err));
					}
				});
			};
			function uploadfile() {
				var file = $api.byId("exampleInputFile");
				//console.log($api.attr(file,"name"));
				//				var formData = new FormData();
				//				var file = $api.byId("exampleInputFile");
				//				formData.append("userid", $api.val($api.byId("exampleInputUserid")));
				//				formData.append("uploadfile", $api.val(file));
				api.ajax({
					url : "http://192.168.99.157/app/api/data/SaveFile",
					method : "post",
					data : {
						values : {
							userid : 'master'
						},
						files : {
							file : $api.val(file)
						}
					}
				}, function(ret, err) {
					if (ret) {
						api.alert({
							msg : JSON.stringify(ret)
						});
					} else {
						api.alert({
							msg : JSON.stringify(err)
						});
					}
				});
			};
		</script>
	</body>
</html>